package cn.yfxu.server.service.impl;

import cn.yfxu.server.pojo.Admin;
import cn.yfxu.server.pojo.Menu;
import cn.yfxu.server.mapper.MenuMapper;
import cn.yfxu.server.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yfxu
 * @since 2021-03-05
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<Menu> getMenusByAdminId() {
        Integer AdminId = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
//        ValueOperations<String,Object> valueOperations=redisTemplate.opsForValue();
//        //从redis获取菜单数据
//        List<Menu> menus =(List<Menu>)valueOperations.get("menu_" + AdminId);
//        //如果为空，从数据库获取
//        if (CollectionUtils.isEmpty(menus)){
//            menus = menuMapper.getMenusByAdminId(AdminId);
//            //将数据设置到redis中
//            valueOperations.set("menu_"+AdminId,menus);
//        }
        List<Menu> menus=menuMapper.getMenusByAdminId(AdminId);
        return menus;
    }

    @Override
    public List<Menu> getMenusWithRole() {

        return menuMapper.getMenusWithRole();
    }
    //查询所有菜单
    @Override
    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }
}
